package com.liyasong.cf.book;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class DataConvert {
	
	public static File toStandard(String filename) throws IOException {
		File file = new File(filename);
		BufferedReader in = new BufferedReader(new FileReader(file));
		File temp = new File("std_"+filename);
		BufferedWriter out = new BufferedWriter(new FileWriter(temp));
		String str, uid;
		String[] strArray;
		while ((str = in.readLine()) != null) {
			strArray = str.split("\\|");
			uid = strArray[0];
			for (int i = 1; i < strArray.length; i+=2) {
				out.write(uid+","+strArray[i]+","+(int)Float.parseFloat(strArray[i+1]));
				out.newLine();
			}
		}
		in.close(); out.close();
		return temp;
	}

	public static File toSimple(String filename) throws IOException {
		File file = new File(filename);
		BufferedReader in = new BufferedReader(new FileReader(file));
		File temp = new File("std_"+filename);
		BufferedWriter out = new BufferedWriter(new FileWriter(temp));
		String str;
		String[] strArray;
		float f;
		while ((str = in.readLine()) != null) {
			strArray = str.split(",");
			if (strArray[2].length() > 6) {
				strArray[2] = strArray[2].substring(0, 6);
			}
			f = Float.parseFloat(strArray[2]);
			if (f <= 1 && f >= -1) {
				out.write(strArray[0]+","+strArray[1]+","+strArray[2]+"\n");
			}
		}
		in.close(); out.close();
		return temp;
	}
	
	public static void main(String[] args) throws IOException {
		long start = System.currentTimeMillis();
		File newfile = toSimple("movieSimFile20120504.txt");
		long end = System.currentTimeMillis();
		
//		System.out.println("文件大小："+file.length()+"B");
		System.out.println("转换为标准文件后大小："+newfile.length()/1024+"Kb");
		System.out.println("转换耗时："+(end-start)+"ms");

	}

}
